/* Copyright 2009 (c) ENSI de Bourges
 * 		88 boulevard Lahitolle, 18020 Bourges Cedex, France
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
/**
 * \file settings-manager.c
 * \brief Source code for the settings_t structure and the settings loading and
 * saving functions.
 * \author Steve Dodier <sidnioulz@gmail.com>
 *
 * This file contains the code for the settings loading and
 * saving functions.
 */
#include <gio/gio.h>

#include "application-data.h"
#include "settings-manager.h"



static gboolean settings_load_key_file (GKeyFile *keyfile, const char *config_dir, const char *data_dir)
{
	char		*path			= NULL;
	int			file_loaded		= 0;
	GError		*err			= NULL;

	// Try to open the configuration file
	path = g_strdup_printf ("%s/"APP_CONF_FILE, config_dir);
	g_key_file_load_from_file (keyfile, path, G_KEY_FILE_KEEP_COMMENTS & G_KEY_FILE_KEEP_TRANSLATIONS, &err);
	g_free (path);

	if (err == NULL) {
		file_loaded = 1;
	}
	// The config directory doesn't have a conf file, try to copy one from the data directory
	else {
		g_clear_error (&err);
		
		path = g_strdup_printf ("%s/"APP_CONF_FILE, data_dir);
		g_key_file_load_from_file (keyfile, path, G_KEY_FILE_KEEP_COMMENTS & G_KEY_FILE_KEEP_TRANSLATIONS, &err);
		g_free (path);
		
		if (err == NULL) {
			file_loaded = 2;
		} else {
			g_warning ("settings_load_from_file: %s", err->message);
			g_clear_error (&err);
		}
	}

	return file_loaded;
}



settings_t *settings_load (const char *config_dir, const char *data_dir)
{
	GError		*err			= NULL;
	GKeyFile	*keyfile		= g_key_file_new ();
	settings_t	*settings		= NULL;

	// Allocate a settings_t struct	
	settings = g_malloc (sizeof (settings_t));
	
	// If a configuration file has been found and loaded
	if (settings_load_key_file (keyfile, config_dir, data_dir)) {
		// Refresh frequency
		settings->refresh_freq = g_key_file_get_integer (keyfile,
							SETTINGS_GROUP_FRAMES, SETTINGS_REFRESH_FREQ_NAME, &err);
		if (err) {
			g_warning ("settings_load_from_file: Error while setting refresh frequency (%s)", err->message);
			settings->refresh_freq = SETTINGS_REFRESH_FREQ_DEFAULT;
			g_clear_error (&err);
		}
		
	}
	// No conf file loaded, using default settings
	else {
		g_warning ("settings_load_from_file: No settings file found, returning default hardcoded settings.");
		settings->refresh_freq = SETTINGS_REFRESH_FREQ_DEFAULT;
	}

	g_key_file_free (keyfile);
	return settings;
}



int settings_save (settings_t *settings)
{
	char				*path		= NULL;
	char				*buffer		= NULL;
	GError				*err		= NULL;
	GFile				*file		= NULL;
	GFileOutputStream	*stream		= NULL;
	GKeyFile			*keyfile	= g_key_file_new ();
	gsize				buff_len	= 0;
	ls_instance_t		*inst		= livesecurity_instance ();

	// We load the current settings file if there is one so the user comments in the file are saved
	settings_load_key_file (keyfile, inst->config_dir, inst->data_dir);

	// Save settings into the KeyFile
	g_key_file_set_integer (keyfile, SETTINGS_GROUP_FRAMES, SETTINGS_REFRESH_FREQ_NAME, settings->refresh_freq);

	// Turn the KeyFile into raw data
	buffer = g_key_file_to_data (keyfile, &buff_len, &err);
	g_key_file_free (keyfile);
	if (err) {
		g_warning ("settings_save: %s", err->message);
		g_clear_error (&err);
		return -1;
	}

	// Write the data to a file via an OutputStream
	path = g_strdup_printf ("%s/"APP_CONF_FILE, inst->config_dir);
	file = g_file_new_for_path (path);
	g_free (path);

	stream = g_file_replace (file, NULL, TRUE, G_FILE_CREATE_NONE, NULL, &err);
	if (err) {
		g_warning ("settings_save: %s", err->message);
		g_clear_error (&err);
		g_free (buffer);
		g_object_unref (file);
		return -1;
	}
	
	if (g_output_stream_write (G_OUTPUT_STREAM (stream), buffer, buff_len, NULL, &err) == -1) {
		g_warning ("settings_save: %s", err->message);
		g_clear_error (&err);
		g_free (buffer);
		g_object_unref (stream);
		g_object_unref (file);
		return -1;
	}
	
	g_free (buffer);
	g_object_unref (stream);
	g_object_unref (file);
	return 0;
}
